package com.medical.medical.controller.clcontroller;

import com.alibaba.fastjson.JSONArray;
import com.medical.medical.entity.*;
import com.medical.medical.service.DoctorService;
import com.medical.medical.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @authorDesc 古娜拉黑暗之神!
 * @author ChenLi
 * @date 2022-09-15 14:45:00
 * @version 1.0.0
 * @description 医生填写就诊报告请求层
 */

@Controller
@Slf4j
@RequestMapping("writereport")
public class WriteReportController {
    @Resource
    private DoctorService doctorService;
    @Resource
    private RecordService recordService;
    @Resource
    private DrugNumberAndId drugNumberAndId;

    /**
     * @description 查看就诊人
     * @author ChenLi
     * @date 2022-09-14 18:04:20
     * @param
     * @return {@link String}
     */
    @RequestMapping("intobookperson")
    public String intobookpersont(int dId,int department,Model model){
        List<Patient> patientList = doctorService.queryPatient(dId,department);
        log.info("就诊人:{}",patientList);
        model.addAttribute("patientList",patientList);
        log.info("进入doctor/bookperson页面");
        return "doctor/bookperson";
    }

    /**
      * @description 获取科室
      * @author ChenLi
      * @date 2022-09-27 00:26:22
      * @param
      * @return {@link Object}
      */
    @RequestMapping("todDepartment")
    @ResponseBody
    public Object getDepartment(int doctorId){
        String departmentName = doctorService.getDepartment(doctorId);
        log.info("科室:{}",departmentName);
        return JSONArray.toJSONString(departmentName);
    }

    /**
     * @description 进入doctor/report页面
     * @author ChenLi
     * @date 2022-09-14 18:04:20
     * @param
     * @return {@link String}
     */
    @RequestMapping("intoreport")
    public String intoreport(String name,String gender,Model model){
        model.addAttribute("name",name);
        model.addAttribute("gender",gender);
        log.info("选择就诊人:{}",name);
        log.info("进入doctor/report页面");
        return "doctor/report";
    }

    /**
      * @description 添加就诊报告
      * @author ChenLi
      * @date 2022-09-15 16:12:56
      * @param
     * @param
      * @return {@link boolean}
      */
    @RequestMapping(value="towritereport",method=RequestMethod.POST)
    @ResponseBody
    public Object WriteReport(Record record,
                              @RequestParam("drugNumberList") List<Integer> drugNumberList,
                              @RequestParam("drugIdList") List<Integer> drugIdList){
        log.info("record={}",record);
        log.info("添加就诊信息");
        String flag = "fail";
        //获取当天日期，并将日期转化成数值
        long time=new Date().getTime();
        Date dates=new Date(time);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
        String times=sdf.format(dates);
        long ndate=Long.parseLong(times);
        log.info("{}",ndate);

        //设置就诊编号
        record.setRecordOrdinal(ndate);
        record.setRecordTime(new Date());
        boolean recordFlag = doctorService.addrecord(record);
        log.info("添加诊断报告信息:{}",recordFlag);

        Record record1 = recordService.viewByRecordOrdinal(ndate);
        log.info("诊断报告:{}",record1);
        int recordId = record1.getRecordId();
        log.info("诊断报告ID:{}",recordId);

        log.info("Id:{}",drugIdList);
        log.info("数量:{}",drugNumberList);

        boolean drugFlag = false;
        List<DrugNumberAndId> drugNumberAndIdList = new ArrayList<>();
        for (int i = 0; i < drugNumberList.size(); i++) {
            DrugNumberAndId drugNumberAndId = new DrugNumberAndId();
            drugNumberAndId.setDrugNumber(drugNumberList.get(i));
            drugNumberAndId.setDrugId(drugIdList.get(i));
            log.info("遍历Id:{}",drugNumberAndId.getDrugId());
            log.info("遍历数量:{}",drugNumberAndId.getDrugNumber());
            drugNumberAndIdList.add(drugNumberAndId);
            log.info("遍历集合:{}",drugNumberAndIdList);

        }
        log.info("集合:{}",drugNumberAndIdList);
        drugFlag = doctorService.adddrug(recordId,drugNumberAndIdList);

        log.info("添加药品信息:{}",drugFlag);
        if (recordFlag && drugFlag){
            log.info("医生添加患者诊断报告信息成功...");
            flag = "success";
        }
        log.info("flag={}",flag);
        return JSONArray.toJSONString(flag);
    }

    /**
      * @description 获取药品编号
      * @author ChenLi
      * @date 2022-09-16 09:05:04
      * @param drugName
      * @return {@link String}
      */
    @RequestMapping("drugnum")
    @ResponseBody
    public Object getDrugNum(String drugName){
        List<String> drugNumList =  doctorService.getDrugNum(drugName);
        log.info("{}",drugNumList);
        return JSONArray.toJSONString(drugNumList);
    }

    /**
      * @description 获取药品所有信息
      * @author ChenLi
      * @date 2022-09-16 10:16:19
      * @param
      * @return {@link String}
      */
    @RequestMapping("drug")
    @ResponseBody
    public Object getDrug(String drugName, String drugNum){
        Drug drug = doctorService.getDrug(drugName,drugNum);
        return JSONArray.toJSONString(drug);
    }
}
